<?php
/* SVN FILE: $Id: statistics_season_players_controller.php 320 2010-07-14 13:56:02Z gregory.rome $ */
/**
 * StatisticsSeasonPlayers Controller.
 *
 * cBBweb : Digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 * @version       $Revision: 320 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-07-14 13:56:02 +0000 (Wed, 14 Jul 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * StatisticsSeasonPlayers Controller.
 *
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 */
class StatisticsSeasonPlayersController extends AppController {

	var $name = 'StatisticsSeasonPlayers';
	var $paginate = array(
  	'StatisticsSeasonPlayer' => array(
  		'order' => array(
				'StatisticsSeasonPlayer.Inflicted_iTouchdowns' => 'desc',
				'Player.iExperience' => 'desc',
				'Player.iValue' => 'desc'),
			'limit' => 10,
  		'contain' => array(
				'Player' => array(
					'fields' => array(
						'Player.ID',
						'Player.strName',
						'Player.idTeam_Listing',
						'Player.iValue',
						'Player.iExperience'),
					'Team' => array(
						'fields' => array('Team.ID, Team.strName'),
						'Coach',
						'Race' => array(
							'LocalizedString'
						)
					),
					'PlayerType' => array(
						'fields' => array('PlayerType.ID'),
						'LocalizedString'
					)))));

	/**
	 *
	 * @todo Enter description here ...
	 */
	function index() {
		if (!$this->championship) {
			$this->Session->setFlash(__('You must select a championship', true));
			$this->set('noview', true);
			return;
		} else {
			$this->set('noview', false);

			$statisticsSeasonPlayers = $this->paginate('StatisticsSeasonPlayer', array(
				'StatisticsSeasonPlayer.bFinished' => 0,
				'StatisticsSeasonPlayer.idRule_Listing' => $this->championship,
				'StatisticsSeasonPlayer.iSeason' => $this->current['Championship']['iSeason']
			));

			$this->set('statisticsSeasonPlayers', $statisticsSeasonPlayers);
		}
	}

	/** Get Best Players.
	 * @param field
	 * @param limit
	 */
	function get_bestplayers($field='td',$limit=5,$season='current',$championship=false)
	{
		if(isset($this->passedArgs['championship'])) {
			$championship = $this->passedArgs['championship'];
		}
		if(isset($this->passedArgs['season'])) {
			$season = $this->passedArgs['season'];
		}
		if(isset($this->passedArgs['limit'])) {
			$limit = $this->passedArgs['limit'];
		}
		if(isset($this->passedArgs['field'])) {
			$field = $this->passedArgs['field'];
		}

		switch ($field) {
			case 'td':
				$field = 'Inflicted_iTouchdowns';
				$fieldName = __('TD', true);
				$fieldTitle = __('Touchdowns', true);
				break;
			case 'pa':
				$field = 'Inflicted_iPasses';
				$fieldName = __('Pa', true);
				$fieldTitle = __('Passes', true);
				break;
			case 'inj':
				$field = 'Inflicted_iInjuries';
				$fieldName = __('Inj', true);
				$fieldTitle = __('Injuries', true);
				break;
			case 'mr':
				$field = 'Inflicted_iMetersRunning';
				$fieldName = __('MR', true);
				$fieldTitle = __('Meters Running', true);
				break;
			case 'cas':
				$field = 'Inflicted_iCasualties';
				$fieldName = __('Cas', true);
				$fieldTitle = __('Casualties', true);
				break;
			case 'dd':
				$field = 'Inflicted_iDead';
				$fieldName = __('Dd', true);
				$fieldTitle = __('Deads', true);
				break;
			case 'mp':
				$field = 'Inflicted_iMetersPassing';
				$fieldName = __('MP', true);
				$fieldTitle = __('Meters Passing', true);
				break;
			case 'ca':
				$field = 'Inflicted_iCatches';
				$fieldName = __('Ca', true);
				$fieldTitle = __('Catches', true);
				break;
			case 'int':
				$field = 'Inflicted_iInterceptions';
				$fieldName = __('Int', true);
				$fieldTitle = __('Interceptions', true);
				break;
			case 'sinj':
				$field = 'Sustained_iInjuries';
				$fieldName = __('sInj', true);
				$fieldTitle = __('Sustained Injuries', true);
				$fieldOrder = 'asc';
				break;
			case 'scas':
				$field = 'Sustained_iCasualties';
				$fieldName = __('sCas', true);
				$fieldTitle = __('Sustained Casualties', true);
				$fieldOrder = 'asc';
				break;
			default:
				$this->Session->setFlash(__('Wrong field', true));
				return;
		}

		if( !isset($fieldOrder)) {
			$fieldOrder = 'desc';
			$options['StatisticsSeasonPlayer.'.$field.' <>'] = 0;
		}

		if( !$championship ) {
			$championship = $this->championship;
		}

		if ( $championship )
		{
			if($season == 'current') {
				$options['StatisticsSeasonPlayer.bFinished'] = 0;
			} else {
				$options['StatisticsSeasonPlayer.iSeason'] = $season;
			}
			$options['StatisticsSeasonPlayer.idRule_Listing'] = $championship;
			$this->set('noview', false);
		} else {
			$this->Session->setFlash(__('You must select a championship', true));
			$this->set('noview', true);
			return;
		}

		$statisticsSeasonPlayers = $this->StatisticsSeasonPlayer->find('all', array(
			'fields' => array('StatisticsSeasonPlayer.'.$field, 'StatisticsSeasonPlayer.idPlayer_Listing'),
			'order' => array(
				'StatisticsSeasonPlayer.'.$field => 'desc',
				'Player.iValue' => 'desc',
				'Player.iExperience' => 'desc',
			),
			'limit' => $limit,
			'contain' => array(
				'Player' => array(
					'fields' => array(
						'Player.ID',
						'Player.strName',
						'Player.idTeam_Listing',
						'Player.iValue',
						'Player.iExperience'),
					'Team' => array(
						'fields' => array('Team.ID', 'Team.strName', 'Team.idCoach'),
						'Coach',
						'Race' => array(
							'LocalizedString')),
					'PlayerType' => array(
						'fields' => array('PlayerType.ID'),
						'LocalizedString'))),
			'conditions' => $options,
		));

		if (isset ($this->params['requested'])) {
			return $statisticsSeasonPlayers;
		} else {
			$this->set('statisticsSeasonPlayers', $statisticsSeasonPlayers);
			$this->set('field', $field);
			$this->set('fieldName', $fieldName);
			$this->set('fieldTitle', $fieldTitle);
		}
	}

}
?>